//
//  RoundManager.cpp
//  DiceWar
//
//  Created by xiaofeng on 22/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include "RoundManager.h"
#include "cocos2d.h"

RoundManager::RoundManager()
: m_currentPlayerIndex(0)
{
    
}

RoundManager::~RoundManager()
{
    
}

void RoundManager::init(int numPlayers)
{
    CCAssert(numPlayers < kInvalidPlayer, "Invalid number of players!");
    for (int index = 0; index < numPlayers; index++)
        m_sequenceVec.push_back(static_cast<PlayerType>(index));
    
    std::random_shuffle(m_sequenceVec.begin(), m_sequenceVec.end());
    m_currentPlayerIndex = 0;
}

void RoundManager::gotoNextPlayer()
{
    m_currentPlayerIndex++;
    if (m_currentPlayerIndex == getNumPlayers()) m_currentPlayerIndex = 0;
}

PlayerType RoundManager::getCurrentPlayer() const
{
    return m_sequenceVec[m_currentPlayerIndex];
}

void RoundManager::removePlayer(PlayerType player)
{
    PlayerSequenceVec::iterator iter = std::find(m_sequenceVec.begin(), m_sequenceVec.end(), player);
    if (iter == m_sequenceVec.end()) CCLOG("The player can't be found in the round manager!");
    m_sequenceVec.erase(iter);
}

int RoundManager::getNumPlayers() const
{
    return static_cast<int>(m_sequenceVec.size());
}
